//
//  ResetPassWordContriller.m
//  ShrimpCrowdordering
//
//  Created by 骆锴 on 2018/8/24.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "ResetPassWordController.h"

@interface ResetPassWordController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UITextField       *passWord;
@property (nonatomic, strong) UITextField       *changePassWord;
@property (nonatomic, strong) UITextField       *confirmPassWord;
@property (nonatomic, strong) NSMutableString   *code;

@end

@implementation ResetPassWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    _code = [NSMutableString string];
    self.title = @"修改密码";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(resignKey)];
    [self.view addGestureRecognizer:tap];
    
    UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49), SCREEN_WIDTH, ScreenX375(49))];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setBackgroundImage:[UIImage imageNamed:@"按钮背景"] forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = Kfont(18);
    [saveBtn addTarget:self action:@selector(sentValue) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:saveBtn];
    
    [self setTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setTableView{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - ScreenX375(49))];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = REXADECIMALCOLOR(@"#eeeeee");
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"setCell";
    NSArray *titleValue = [[NSArray alloc] initWithObjects:@"原密码",@"新密码",@"确认新密码", nil];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    UILabel *titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(16), ScreenX375(16), ScreenX375(100), ScreenX375(15))];
    titleLabel.text = [titleValue objectAtIndex:indexPath.row];
    titleLabel.font = Kfont(15);
    titleLabel.textColor = REXADECIMALCOLOR(@"#333333");
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [cell addSubview:titleLabel];
    
    NSArray *placeHold = [[NSArray alloc] initWithObjects:@"请输入原密码",@"请输入新密码",@"请再输入新密码", nil];
    if (indexPath.row == 0) {
        
        UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
        UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
        NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
        [bar setItems:buttonsArray];
        
        _passWord = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(220), ScreenX375(15))];
        _passWord.font = Kfont(15);
        _passWord.textAlignment = NSTextAlignmentLeft;
        _passWord.placeholder = [placeHold objectAtIndex:indexPath.row];
        _passWord.secureTextEntry = YES;
        _passWord.inputAccessoryView = bar;
        [cell addSubview:_passWord];
    }else if(indexPath.row == 1){
        UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
        UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
        NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
        [bar setItems:buttonsArray];
        
        _changePassWord = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(220), ScreenX375(15))];
        _changePassWord.font = Kfont(15);
        _changePassWord.textAlignment = NSTextAlignmentLeft;
        _changePassWord.placeholder = [placeHold objectAtIndex:indexPath.row];
        _changePassWord.secureTextEntry = YES;
        _changePassWord.inputAccessoryView = bar;
        [cell addSubview:_changePassWord];
    }else{
        UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH,ScreenX375(44))];
        UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKey)];
        NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
        [bar setItems:buttonsArray];
        
        _confirmPassWord = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(105), ScreenX375(16), ScreenX375(220), ScreenX375(15))];
        _confirmPassWord.font = Kfont(15);
        _confirmPassWord.textAlignment = NSTextAlignmentLeft;
        _confirmPassWord.placeholder = [placeHold objectAtIndex:indexPath.row];
        _confirmPassWord.secureTextEntry = YES;
        _confirmPassWord.inputAccessoryView = bar;
        [cell addSubview:_confirmPassWord];
    }
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(47.5), ScreenX375(359), ScreenX375(0.5))];
    line.backgroundColor = LineColor;
    [cell addSubview:line];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScreenX375(48);
}

-(void)sentValue{
    if (![_changePassWord.text isEqualToString:_confirmPassWord.text]) {
        [HttpManager showNoteMsg:@"两次密码不相同"];
    }else{
        NSDictionary *parameter = @{@"userId":kNSUDefaultReadKey(@"userId"),@"oldPassword":_passWord.text,@"newPassword":_changePassWord.text};
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/updatePassword",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
            NSString *code = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
            if ([code isEqualToString:@"200"]) {
                self.code = successResponse[@"data"][@"code"];
            }else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"message"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }
}

-(void)resignKey{
    [_passWord resignFirstResponder];
    [_changePassWord resignFirstResponder];
    [_confirmPassWord resignFirstResponder];
}

@end
